<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="psn" prop="psn">
        <el-input v-model="queryParams.psn" placeholder="请输入psn" clearable size="small" style="width: 200px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="barcode" prop="barcode">
        <el-input v-model="queryParams.barcode" placeholder="请输入barcode" clearable size="small" style="width: 200px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="09条码" prop="bar09">
        <el-input v-model="queryParams.bar09" placeholder="请输入bar09" clearable size="small" style="width: 200px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="锁定状态" prop="lockFlag">
        <el-select v-model="queryParams.lockFlag" placeholder="请选择锁定状态" style="width: 200px"
          @keyup.enter.native="handleQuery">
          <el-option v-for="item in statusOptions" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['partition:partitionBD:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['partition:partitionBD:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['partition:partitionBD:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['partition:partitionBD:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partitionList" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column type="selection" width="60" align="center" fixed="left" />
      <el-table-column label="本地隔离组件id" align="center" width="100" fixed="left" prop="pkid" v-if="false" />
      <el-table-column label="隔离单" align="center" prop="segment5" width="100" fixed="left" />
      <el-table-column label="psn" align="center" prop="psn" width="100" />
      <el-table-column label="barcode" align="center" prop="barcode" width="100" />
      <el-table-column label="09条码" align="center" prop="bar09" width="100" />
      <el-table-column label="编码" align="center" prop="item" width="100" />
      <el-table-column label="lotcode" align="center" prop="lotcode" width="100" />
      <el-table-column label="datecode" align="center" prop="datecode" width="100" />
      <el-table-column label="锁定状态" align="center" prop="lockFlag" :formatter="getStatusName" width="100" />
      <el-table-column label="供应商代码" align="center" prop="segment1" width="140" />
      <el-table-column label="供应商" align="center" prop="segment4" width="200" />
      <el-table-column label="Lot通配符" align="center" prop="segment2" width="140" />
      <el-table-column label="d/c通配符" align="center" prop="segment3" width="140" />
      <el-table-column label="创建人" align="center" prop="createdBy" width="100" />
      <el-table-column label="创建时间" align="center" prop="createdDate" width="160" />
      <el-table-column label="更新人" align="center" prop="updatedBy" width="100" />
      <el-table-column label="更新时间" align="center" prop="updatedDate" width="160" />
      <el-table-column label="备注" align="center" prop="comments" width="200" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" style="margin-right: 9px"
            @click="handleUpdate(scope.row)" v-hasPermi="['partition:partitionBD:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" style="margin-right: 9px"
            @click="handleDelete(scope.row)" v-hasPermi="['partition:partitionBD:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-scissors" @click="handleLock(scope.row)"
            v-hasPermi="['partition:partitionBD:lock']">锁定</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改补料记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="psn" prop="psn">
              <el-input v-model="form.psn" placeholder="请输入psn" :disabled="yys" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="barcode" prop="barcode">
              <el-input v-model="form.barcode" placeholder="请输入barcode" :disabled="yys" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="09条码" prop="bar09">
              <el-input v-model="form.bar09" placeholder="请输入09条码" :disabled="yys" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="lotcode" prop="lotcode">
              <el-input v-model="form.lotcode" placeholder="请输入lotcode" :disabled="yys" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="datecode" prop="datecode">
              <el-input v-model="form.datecode" placeholder="请输入datecode" :disabled="yys" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="供应商" prop="segment1">
              <el-select v-model="form.segment1" placeholder="请选择供应商" style="width: 240px" :disabled="yys" filterable>
                <el-option v-for="item in GYSOptions" :key="item.code" :label="item.name" :value="item.code">
                  <span style="float: left">{{ item.code }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="原因" prop="yy">
              <el-input v-model="form.yy" placeholder="请输入原因" type="textarea" :rows="4" style="width: 240px"
                />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPartitionBD,
    getPartitionBD,
    delReplenish,
    addPartitionBD,
    updatePartitionBD,
    exportReplenish
  } from "@/api/partition/partitionBD";
  import { listCustomer } from "@/api/system/customer";

  export default {
    name: "PartitionBD",
    data() {
      return {
        // 遮罩层
        loading: true,
        // yy
        yys: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 本地隔离表格数据
        partitionList: [],
        // 供应商下拉
        GYSOptions: [],
        // 状态下拉
        statusOptions: [
          { code: 0, name: '未隔离物料' },
          { code: 1, name: '隔离物料' }
        ],
        // 弹出层标题
        title: "",
        separateTitle: "",
        // 是否显示弹出层
        open: false,
        separateOpen: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          psn: undefined,
          barcode: undefined,
          bar09: undefined,
          lockFlag: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          psn: [{ required: true, message: "psn不能为空", trigger: "change" }],
          barcode: [{ required: true, message: "barcode不能为空", trigger: "change" }],
          bar09: [{ required: true, message: "09条码不能为空", trigger: "change" }]
        }
      };
    },
    created() {
      this.getGYSList();
      this.getList();
    },
    methods: {
      /** 查询本地隔离列表 */
      getList() {
        this.loading = true;
        listPartitionBD(this.queryParams).then(response => {
          this.partitionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 查询供应商列表
      getGYSList() {
        listCustomer({  }).then(response => {
          this.GYSOptions = response.rows;
        });
      },
      // 查询状态名称
      getStatusName(row, column) {
        let name = '';
        switch (row.status) {
          case 0:
            name = '未隔离物料';
            break;
          case 1:
            name = '隔离物料';
            break;
          default: console.log('no default')
        }
        return name;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 取消按钮
      separateCancel() {
        this.separateOpen = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.pkid);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.yys = false;
        this.title = "添加隔离记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const plugIds = row.pkid || this.ids;
        getPartitionBD(plugIds).then(response => {
          this.form = response.data;
          this.open = true;
          this.yys = false;
          this.title = "修改隔离记录";
        });
      },
      /** 锁定按钮操作 */
      handleLock(row) {
        this.reset();
        const plugIds = row.pkid || this.ids;
        getPartitionBD(plugIds).then(response => {
          this.form = response.data;
          this.open = true;
          this.yys = true;
          this.title = "锁定隔离记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.pkid != null) {
              updatePartitionBD(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addPartitionBD(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const plugIds = row.pkid 
        this.$confirm('是否确认删除编号为"' + plugIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delReplenish(plugIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () { });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportReplenish(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () { });
      }
    }
  };
</script>